import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { TabStack } from './tabStack';
import Settings from '@/pages/Userpage/Settings';
import SignIn from '@/pages/Auth/SignIn';
import ConfigPass from '@/pages/Auth/ConfigPass';
import ForgetPass from '@/pages/Auth/ForgetPass';
import Register from '@/pages/Auth/Register';
import Privacy from '@/pages/Policy/Privacy';
import Agreement from '@/pages/Policy/Agreement';
import NotFound from '@/pages/NotFound';
import NetworkLoggerView from '@/pages/Policy/NetworkLoggerView';
import ViewLog from '@/pages/Policy/NetworkLoggerView/Log';


/** 未登录 */
const AUTH_SCREENS = [
  {
    name: 'SignIn',
    component: SignIn,
  },
  {
    name: 'ConfigPass',
    component: ConfigPass,
  },
  {
    name: 'ForgetPass',
    component: ForgetPass,
  },
  {
    name: 'Register',
    component: Register,
  },
];

/** 已经登陆 */
const MAIN_SCREENS = [
  {
    name: 'Tab',
    component: TabStack,
    options: {
      headerShown: false,
    },
  },
  {
    name: 'Settings',
    component: Settings,
    options: {
      title: '系统设置',
    },
  },
  {
    name: "NotFound",
    component: NotFound,
    options: {
      title: '页面不存在',
    },
  }
];

/** 协议 */
const MODAL_SCREENS = [
  {
    name: 'Agreement',
    component: Agreement,
    options: {
      title: '用户协议',
    },
  },
  {
    name: 'Privacy',
    component: Privacy,
    options: {
      title: '隐私政策',
    },
  },
  {
    name: 'NetworkLoggerView',
    component: NetworkLoggerView,
    options: {
      title: '网络日志',
    },
  },
];

const Stack = createNativeStackNavigator();

/**
 * @param {Boolean} confirmed 是否同意协议
 * @param {Boolean} signedIn 是否登陆
 */
export default function AppStack({ confirmed, signedIn }: { confirmed: boolean; signedIn: boolean }) {
  return (
    <>

      <Stack.Navigator
        initialRouteName={confirmed ? (signedIn ? 'Tab' : 'SignIn') : 'PrivacyConfirm'}
        screenOptions={{
          gestureEnabled: true,
          gestureDirection: 'horizontal',
          headerTitleAlign: 'center',
          animation: 'slide_from_right',
          animationDuration: 200,
          headerBackTitleVisible: false,
        }}
      >
        {/* {!confirmed && <Stack.Screen name="PrivacyConfirm" component={PrivacyConfirm} options={{ headerShown: false }} />} */}
        {signedIn ? (
          <Stack.Group
            screenOptions={{
              presentation: 'card',
            }}
          >
            {MAIN_SCREENS.map(screen => (
              <Stack.Screen key={screen.name} {...screen} />
            ))}
          </Stack.Group>
        ) : (
          <Stack.Group screenOptions={{ headerShown: false, presentation: 'card' }}>
            {AUTH_SCREENS.map(screen => (
              <Stack.Screen key={screen.name} {...screen} />
            ))}
          </Stack.Group>
        )}
        <Stack.Group
          screenOptions={{
            presentation: 'transparentModal',
            animation: 'slide_from_bottom',
            animationDuration: 200,
            animationTypeForReplace: 'pop',
          }}
        >
          {MODAL_SCREENS.map(screen => (
            <Stack.Screen key={screen.name} {...screen} />
          ))}
        </Stack.Group>
      </Stack.Navigator>

      {!__DEV__ ? null : <ViewLog />}
    </>
  );
}
